package receivers;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public abstract class Receiver {
    protected Connection connection;
    protected final Channel channel;
    protected String queueName;
    protected boolean persistence;
    protected String exchangeName;
    protected String exchangeType;
    protected String routingKey;


    public  Receiver(
            Connection connection,
            String queueName,
            boolean persistence,
            String exchangeName,
            String exchangeType,
            String routingKey) throws IOException {
        this.connection = connection;
        this.channel = connection.createChannel();
        this.queueName = queueName;
        this.persistence = persistence;
        this.exchangeName = exchangeName;
        this.exchangeType = exchangeType;
        this.routingKey = routingKey;
    }

    public void closeConnection() throws IOException {
        connection.close();
    }

    public void closeChannel() throws IOException, TimeoutException {
        channel.close();
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public Channel getChannel() {
        return channel;
    }

    public String getQueueName() {
        return queueName;
    }

    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    public boolean isPersistence() {
        return persistence;
    }

    public void setPersistence(boolean persistence) {
        this.persistence = persistence;
    }

    public String getExchangeName() {
        return exchangeName;
    }

    public void setExchangeName(String exchangeName) {
        this.exchangeName = exchangeName;
    }

    public String getExchangeType() {
        return exchangeType;
    }

    public void setExchangeType(String exchangeType) {
        this.exchangeType = exchangeType;
    }

    public String getRoutingKey() {
        return routingKey;
    }

    public void setRoutingKey(String routingKey) {
        this.routingKey = routingKey;
    }

    public abstract void receive(Consumer consumer) throws IOException;
}
